/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLEUR;

import CONTROLEUR.exceptions.NonexistentEntityException;
import CONTROLEUR.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MODEL.Client;
import MODEL.Reserver2;
import MODEL.Reserver2PK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author ahmed
 */
public class Reserver2JpaController implements Serializable {

    public Reserver2JpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Reserver2 reserver2) throws PreexistingEntityException, Exception {
        if (reserver2.getReserver2PK() == null) {
            reserver2.setReserver2PK(new Reserver2PK());
        }
        reserver2.getReserver2PK().setIdC(reserver2.getClient().getIdC());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Client client = reserver2.getClient();
            if (client != null) {
                client = em.getReference(client.getClass(), client.getIdC());
                reserver2.setClient(client);
            }
            em.persist(reserver2);
            if (client != null) {
                client.getReserver2Collection().add(reserver2);
                client = em.merge(client);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findReserver2(reserver2.getReserver2PK()) != null) {
                throw new PreexistingEntityException("Reserver2 " + reserver2 + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Reserver2 reserver2) throws NonexistentEntityException, Exception {
        reserver2.getReserver2PK().setIdC(reserver2.getClient().getIdC());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reserver2 persistentReserver2 = em.find(Reserver2.class, reserver2.getReserver2PK());
            Client clientOld = persistentReserver2.getClient();
            Client clientNew = reserver2.getClient();
            if (clientNew != null) {
                clientNew = em.getReference(clientNew.getClass(), clientNew.getIdC());
                reserver2.setClient(clientNew);
            }
            reserver2 = em.merge(reserver2);
            if (clientOld != null && !clientOld.equals(clientNew)) {
                clientOld.getReserver2Collection().remove(reserver2);
                clientOld = em.merge(clientOld);
            }
            if (clientNew != null && !clientNew.equals(clientOld)) {
                clientNew.getReserver2Collection().add(reserver2);
                clientNew = em.merge(clientNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Reserver2PK id = reserver2.getReserver2PK();
                if (findReserver2(id) == null) {
                    throw new NonexistentEntityException("The reserver2 with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Reserver2PK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reserver2 reserver2;
            try {
                reserver2 = em.getReference(Reserver2.class, id);
                reserver2.getReserver2PK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The reserver2 with id " + id + " no longer exists.", enfe);
            }
            Client client = reserver2.getClient();
            if (client != null) {
                client.getReserver2Collection().remove(reserver2);
                client = em.merge(client);
            }
            em.remove(reserver2);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Reserver2> findReserver2Entities() {
        return findReserver2Entities(true, -1, -1);
    }

    public List<Reserver2> findReserver2Entities(int maxResults, int firstResult) {
        return findReserver2Entities(false, maxResults, firstResult);
    }

    private List<Reserver2> findReserver2Entities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Reserver2.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Reserver2 findReserver2(Reserver2PK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Reserver2.class, id);
        } finally {
            em.close();
        }
    }

    public int getReserver2Count() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Reserver2> rt = cq.from(Reserver2.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
